import cn.hutool.json.JSONUtil;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.moderation.v2.ModerationClient;
import com.huaweicloud.sdk.moderation.v2.region.ModerationRegion;
import com.huaweicloud.sdk.moderation.v2.model.RunImageBatchModerationRequest;
import com.huaweicloud.sdk.moderation.v2.model.ImageBatchModerationReq;
import com.huaweicloud.sdk.moderation.v2.model.RunImageBatchModerationResponse;

import java.util.List;
import java.util.ArrayList;

public class RunImageBatchModerationSolution {
    public static void main(String[] args) {
        String ak = "HYA7PFYVSZSJ3BO8WN03";
        String sk = "InNGxyn2bcSy75Pa5RK4oyo0JD0DbKM9MRi1XjId";

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);

        ModerationClient client = ModerationClient.newBuilder()
                .withCredential(auth)
                .withRegion(ModerationRegion.valueOf("cn-east-3"))
                .build();
        RunImageBatchModerationRequest request = new RunImageBatchModerationRequest();
        ImageBatchModerationReq body = new ImageBatchModerationReq();
        List<ImageBatchModerationReq.CategoriesEnum> listbodyCategories = new ArrayList<>();
        listbodyCategories.add(ImageBatchModerationReq.CategoriesEnum.fromValue("politics"));
        listbodyCategories.add(ImageBatchModerationReq.CategoriesEnum.fromValue("terrorism"));
        listbodyCategories.add(ImageBatchModerationReq.CategoriesEnum.fromValue("porn"));
        List<String> listbodyUrls = new ArrayList<>();
        listbodyUrls.add("https://sdk-obs-source-save.obs.cn-north-4.myhuaweicloud.com/terrorism.jpg");
        body.withThreshold((double) 0);
        body.withCategories(listbodyCategories);
        body.withUrls(listbodyUrls);
        request.withBody(body);
        try {
            RunImageBatchModerationResponse response = client.runImageBatchModeration(request);
            System.out.println(response.getHttpStatusCode());
            System.out.println(JSONUtil.toJsonStr(response));
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}
